const SCBroker = require('socketcluster/scbroker');
const scClusterBrokerClient = require('scc-broker-client');
const scRedis = require('sc-redis');

export class Broker extends SCBroker {
  run() {
    console.log('   >> Broker PID:', process.pid);

    // This is defined in server.js (taken from environment constiable SC_CLUSTER_STATE_SERVER_HOST).
    // If this property is defined, the broker will try to attach itself to the SC cluster for
    // automatic horizontal scalability.
    // This is mostly intended for the Kubernetes deployment of SocketCluster - In this case,
    // The clustering/sharding all happens automatically.

    if(this.options.clusterStateServerHost) {
      scClusterBrokerClient.attach(this, {
        stateServerHost: this.options.clusterStateServerHost,
        stateServerPort: this.options.clusterStateServerPort,
        mappingEngine: this.options.clusterMappingEngine,
        clientPoolSize: this.options.clusterClientPoolSize,
        authKey: this.options.clusterAuthKey,
        stateServerConnectTimeout: this.options.clusterStateServerConnectTimeout,
        stateServerAckTimeout: this.options.clusterStateServerAckTimeout,
        stateServerReconnectRandomness: this.options.clusterStateServerReconnectRandomness
      });
    }

    if(this.options.brokerOptions && this.options.brokerOptions.host) {
      scRedis.attach(this);
    }
  }
}

const broker = new Broker();
